#include "los_memory.h"
#include "los_config.h"
#include "los_task.h"

#define TEST_POOL_SIZE (4*1024)
__attribute__((aligned(4))) UINT8 g_testPoolExample[TEST_POOL_SIZE];

#if (LOSCFG_MEM_MUL_REGIONS == 1)
extern CHAR __memory1_start;
extern CHAR __memory2_start;
extern CHAR __memory3_start;
extern CHAR __memory4_start;

LOS_MEM_REGION multipleMemRegions[] =
 {
  { (UINT8 *)&__memory1_start, 0x1000 },
  { (UINT8 *)&__memory2_start, 0x10000 },
  { (UINT8 *)&__memory3_start, 0x200 },
//   { (UINT8 *)&__memory4_start, 0x1200 },
  { NULL, 0 }
 };
#endif

VOID Example_DynMem(VOID)
{
    UINT32 *mem = NULL;
    UINT32 ret;

    /*初始化内存池*/
    ret = LOS_MemInit(g_testPoolExample, TEST_POOL_SIZE);
    if (LOS_OK  == ret) {
        printf("Mem init success!\n");
    } else {
        printf("Mem init failed!\n");
        return;
    }

    /*分配内存*/
    mem = (UINT32 *)LOS_MemAlloc(g_testPoolExample, 4);
    if (NULL == mem) {
        printf("Mem alloc failed!\n");
        return;
    }
    printf("Mem alloc success!\n");

    /*赋值*/
    *mem = 828;
    printf("*mem = %d\n", *mem);

    /*释放内存*/
    ret = LOS_MemFree(g_testPoolExample, mem);
    if (LOS_OK == ret) {
        printf("Mem free success!\n");
    } else {
        printf("Mem free failed!\n");
    }

    return;
}
UINT32 ExampleDynMemEntry(VOID) 
{     
    UINT32 ret;     
    TSK_INIT_PARAM_S initParam = {0};
    initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_DynMem;
    initParam.usTaskPrio = 10; 
    initParam.pcName = "Example_DynMem";
    initParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;

    /* 创建高优先级任务，由于锁任务调度，任务创建成功后不会马上执行 */
    UINT32 g_taskHiID;
    ret = LOS_TaskCreate(&g_taskHiID, &initParam);
    if (ret != LOS_OK) {
        LOS_TaskUnlock();
        PRINTK("Example_DynMem create Failed! ret=%d\n", ret);
        return LOS_NOK;
    }      
    PRINTK("Example_DynMem create Success!\n");
    // while(1){};
    return LOS_OK;
}